<template>
    <el-drawer title="订单详情" v-model="drawerVisible" direction="rtl" size="50%">
        <el-card shadow="never" :body-style="{ padding: '20px' }" class="mb-3">
            <template #header>
                <b class="text-sm">订单详情</b>
            </template>
            <el-form label-width="80px" label-position="left">
                <el-form-item label="订单号">{{ info.no }}</el-form-item>
                <el-form-item label="付款方式">{{ info.payment_method }}</el-form-item>
                <el-form-item label="支付时间">{{ info.paid_time }}</el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '20px' }" class="mb-3" v-if="info.ship_data">
            <template #header>
                <b class="text-sm">发货信息</b>
            </template>
            <el-form label-width="80px" label-position="left">
                <el-form-item label="物流公司">{{ info.ship_data.express_company }}</el-form-item>
                <el-form-item label="运单号">{{ info.ship_data.express_no}}
                    <el-button type="primary" text size="small" @click="openShipInfoModal(info.id) " class="ml-3" :loading="loading">查看物流</el-button>
                </el-form-item>
                <el-form-item label="发货时间">{{  ship_time}}</el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '20px' }" class="mb-3">
            <template #header>
                <b class="text-sm">商品信息</b>
            </template>
            <div class="flex mb-2" v-for="(item,index) in info.order_items" :key="index">
                <el-image :src="item.goods_item?.cover ?? ''" fit="cover" :lazy="true" style="width: 60px; height: 60px;"></el-image>
                <div class="ml-2 text-sm">
                    <p>{{ item.goods_item?.title ?? '商品已经被删除' }}</p>
                    <p v-if="item.sku" class="mt-1">
                        <el-tag type="info" size="small"  >{{ item.sku }}</el-tag>
                    </p>
                    <p>
                         <b class="text-rose-500 mr-2">￥{{ item.price }}</b>
                         <span class="text-xs text-gray-500">x{{ item.num }}</span>
                    </p>
                </div>
            </div>
            <el-form label-width="80px" label-position="left">
                <el-form-item label="成交价" class="text-rose-500 font-bold">￥{{ info.total_price }}</el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '20px' }" v-if="info.address" class="mb-3">
            <template #header>
                <b class="text-sm">收货信息</b>
            </template>
            <el-form label-width="80px" label-position="left">
                <el-form-item label="收货人">{{ info.address.name }}</el-form-item>
                <el-form-item label="联系方式">{{ info.address.phone}}</el-form-item>
                <el-form-item label="收货地址">{{ info.address.province + info.address.city + info.address.district + info.address.address }}</el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '20px' }" v-if="info.refund_status != 'pending'" class="mb-3">
            <template #header>
                <b class="text-sm">退款信息</b>
                <el-button text disabled style="float:right"> {{refund_status}}</el-button>
            </template>
            <el-form label-width="80px" label-position="left">
                <el-form-item label="退款理由">{{ info.extra.refund_reason }}</el-form-item>
            </el-form>
        </el-card>
    </el-drawer>
    <ShipInfoModel ref="shipInfoModelRef" />
</template>

<script setup>
import { ref , computed} from "vue";
import { useDateFormat } from "@vueuse/core"
import ShipInfoModel from "./ShipInfoModel.vue";
const props = defineProps({
    info:Object
})
const ship_time = computed(() => {
    if(props.info.ship_data){
        const x =  useDateFormat(props.info.ship_data.express_time * 1000,"YYYY-MM-DD HH:mm:ss");
        return x.value;
    }
    return ""
    
})
const refund_status = computed(() => {
    const opt = {
        pending:"未退款",
        applied:"已申请，等待审核",
        processing:"退款中",
        success:"退款成功",
        failed:"退款失败"
    }
    return props.info.refund_status ? opt[props.info.refund_status] : '';
})
const drawerVisible = ref(false);

const open = () => {
    drawerVisible.value = true;
}

const shipInfoModelRef = ref(null);
const loading = ref(false);
const openShipInfoModal = (id) => {
    loading.value = true;
    shipInfoModelRef.value.open(id).finally(() => loading.value = false)
}
defineExpose({
    open
})
</script>